/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.gui;

import aptech.semester.code.data.DepartmentList;
import aptech.semester.code.entity.DepartmentEntity;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author vi
 */
public class Department extends javax.swing.JInternalFrame {

    /**
     * Creates new form Position
     */
    public Department(JFrame mainFrame) {
        initComponents();
        initControl();
        txtDeptName.setEditable(false);
        txtDeptDescription.setEditable(false);
        btnSaveDept.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDeptName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDeptDescription = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        btnUpdateDept = new javax.swing.JButton();
        btnAddDept = new javax.swing.JButton();
        btnDeleteDept = new javax.swing.JButton();
        btnSaveDept = new javax.swing.JButton();
        btnExitDept = new javax.swing.JButton();
        btnExportDepartment = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDepartment = new javax.swing.JTable();

        setClosable(true);
        setTitle("Department");
        setPreferredSize(new java.awt.Dimension(800, 600));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel1.setText("Department Name");

        txtDeptName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDeptNameActionPerformed(evt);
            }
        });

        jLabel3.setText("Description");

        txtDeptDescription.setColumns(20);
        txtDeptDescription.setRows(5);
        jScrollPane2.setViewportView(txtDeptDescription);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDeptName, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDeptName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Command", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        btnUpdateDept.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnUpdateDept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/edit.png"))); // NOI18N
        btnUpdateDept.setText("Update");
        btnUpdateDept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateDeptActionPerformed(evt);
            }
        });

        btnAddDept.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnAddDept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/add.png"))); // NOI18N
        btnAddDept.setText("Add New");
        btnAddDept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddDeptActionPerformed(evt);
            }
        });

        btnDeleteDept.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnDeleteDept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/delete.png"))); // NOI18N
        btnDeleteDept.setText("Delete");
        btnDeleteDept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteDeptActionPerformed(evt);
            }
        });

        btnSaveDept.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnSaveDept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/save.png"))); // NOI18N
        btnSaveDept.setText("Save");
        btnSaveDept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveDeptActionPerformed(evt);
            }
        });

        btnExitDept.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnExitDept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/exit.png"))); // NOI18N
        btnExitDept.setText("Exit");
        btnExitDept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitDeptActionPerformed(evt);
            }
        });

        btnExportDepartment.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/report.png"))); // NOI18N
        btnExportDepartment.setText("Export");
        btnExportDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportDepartmentActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnAddDept)
                .addGap(34, 34, 34)
                .addComponent(btnSaveDept)
                .addGap(38, 38, 38)
                .addComponent(btnUpdateDept)
                .addGap(40, 40, 40)
                .addComponent(btnDeleteDept)
                .addGap(34, 34, 34)
                .addComponent(btnExportDepartment)
                .addGap(50, 50, 50)
                .addComponent(btnExitDept)
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnUpdateDept)
                .addComponent(btnAddDept)
                .addComponent(btnDeleteDept)
                .addComponent(btnSaveDept)
                .addComponent(btnExitDept)
                .addComponent(btnExportDepartment))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "List Department", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.ABOVE_TOP, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tbDepartment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Dept_Name", "Description"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbDepartment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbDepartmentMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbDepartment);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 10, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtDeptNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDeptNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDeptNameActionPerformed

    private void btnAddDeptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddDeptActionPerformed
        txtDeptName.setText("");
        txtDeptDescription.setText("");
        txtDeptName.setEditable(true);
        txtDeptDescription.setEditable(true);
        btnAddDept.setEnabled(false);
        btnUpdateDept.setEnabled(false);
        btnDeleteDept.setEnabled(false);
        btnSaveDept.setText("Save Add");
        btnSaveDept.setEnabled(true);
        tbDepartment.setEnabled(false);
    }//GEN-LAST:event_btnAddDeptActionPerformed

    private void tbDepartmentMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDepartmentMousePressed
        int r = tbDepartment.getSelectedRow();
        txtDeptName.setText(tbDepartment.getValueAt(r, 1).toString());
        txtDeptDescription.setText(tbDepartment.getValueAt(r, 2).toString());
    }//GEN-LAST:event_tbDepartmentMousePressed

    private void btnSaveDeptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveDeptActionPerformed
        if(btnSaveDept.getText().equals("Save Add")){
            String name = txtDeptName.getText().trim();
            String description = txtDeptDescription.getText().trim();
            if(name.isEmpty()){
                JOptionPane.showMessageDialog(null, "Department name can not blank");
                txtDeptName.requestFocus();
                return;
            }
            if(txtDeptName.getText().trim().matches("^[\\d]+$")){
                JOptionPane.showMessageDialog(null, "Department name can not numberic");
                txtDeptName.requestFocus();
                return;
            }
            if(name.length() > 50){
                JOptionPane.showMessageDialog(null, "Department name less than 50 character!");
                txtDeptName.requestFocus();
                return;
            }
            if(txtDeptDescription.getText().trim().matches("^[\\d]+$")){
                JOptionPane.showMessageDialog(null, "Description can not numberic");
                txtDeptDescription.requestFocus();
                return;
            }
         
            
            DepartmentEntity d = new DepartmentEntity();
            d.setDepartmentName(txtDeptName.getText().trim());
            d.setDescription( txtDeptDescription.getText().trim());
            
            DepartmentList dl = new DepartmentList();
            if(dl.insert(d)==true){
                JOptionPane.showMessageDialog(null, "Insert successfull!");
                modelDept = (DefaultTableModel) tbDepartment.getModel();
                modelDept.setRowCount(0);
                for (DepartmentEntity item : new DepartmentList().getAllDepartment()) {
                    modelDept.addRow(item.toVector());
                }
                btnSaveDept.setEnabled(false);
                btnAddDept.setEnabled(true);
                btnUpdateDept.setEnabled(true);
                btnDeleteDept.setEnabled(true);
                tbDepartment.setEnabled(true);
                txtDeptName.setEditable(false);
                txtDeptDescription.setEditable(false);
            }else{
                JOptionPane.showMessageDialog(null, "Insert failed!");
                return;
            }
            
        }
        
        if(btnSaveDept.getText().equals("Save Update")){
            int row = tbDepartment.getSelectedRow();
            String a = tbDepartment.getValueAt(row, 0).toString(); 
            int ID = Integer.parseInt(a);
            
            DepartmentEntity d = new DepartmentEntity();
                d.setDepartmentId(ID);
                d.setDepartmentName(txtDeptName.getText().trim());
                d.setDescription(txtDeptDescription.getText().trim());
            
            DepartmentList dl = new DepartmentList();
            if (dl.update(d) == true) {
                JOptionPane.showMessageDialog(null, "Update successfull");
                modelDept = (DefaultTableModel) tbDepartment.getModel();
                modelDept.setRowCount(0);
                for (DepartmentEntity item : new DepartmentList().getAllDepartment()) {
                    modelDept.addRow(item.toVector());
                }
                btnSaveDept.setEnabled(false);
                btnAddDept.setEnabled(true);
                btnUpdateDept.setEnabled(true);
                btnDeleteDept.setEnabled(true);
                tbDepartment.setEnabled(true);
                txtDeptName.setEditable(false);
                txtDeptDescription.setEditable(false);
                
            } else {
                JOptionPane.showMessageDialog(null, "Update failed");
                return;
            }
        }
        
        
    }//GEN-LAST:event_btnSaveDeptActionPerformed

    private void btnUpdateDeptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateDeptActionPerformed
        int n =  tbDepartment.getSelectedRow();
        if(n < 0){
            JOptionPane.showMessageDialog(null, "Please choose row to update!");
            return;
        }else{
            txtDeptName.setEditable(true);
            txtDeptDescription.setEditable(true);
            btnAddDept.setEnabled(false);
            btnDeleteDept.setEnabled(false);
            btnSaveDept.setText("Save Update");
            btnSaveDept.setEnabled(true);
            btnUpdateDept.setEnabled(false);
            tbDepartment.setEnabled(false);
        }
    }//GEN-LAST:event_btnUpdateDeptActionPerformed

    private void btnDeleteDeptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteDeptActionPerformed
     // lay chi so dong dc chon tren jtable
        int i = tbDepartment.getSelectedRow();
     if(i<0){
         JOptionPane.showMessageDialog(null, "Please choose row to delete!");
     }else{
         int r = JOptionPane.showConfirmDialog(null, "Are you sure ?","Notify",JOptionPane.YES_NO_OPTION);
         if(r == JOptionPane.OK_OPTION){
             int id = Integer.parseInt(tbDepartment.getValueAt(i, 0).toString());
             if(new DepartmentList().delete(id) == true){
                 JOptionPane.showMessageDialog(null, "Delete successfull!");
                 modelDept = (DefaultTableModel) tbDepartment.getModel();
                 modelDept.setRowCount(0);
                 for (DepartmentEntity item : new DepartmentList().getAllDepartment()) {
                     modelDept.addRow(item.toVector());
                 }
                 return;
             }else{
                 JOptionPane.showMessageDialog(null, "Delete failed!");
                 return;
             }
         }
     }
    }//GEN-LAST:event_btnDeleteDeptActionPerformed

    private void btnExitDeptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitDeptActionPerformed
       System.exit(0);
    }//GEN-LAST:event_btnExitDeptActionPerformed

    private void btnExportDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportDepartmentActionPerformed
        MessageFormat headerFormat= new MessageFormat("Department Report");
            MessageFormat footerFormat = new  MessageFormat("Page {0}");
        try {
            tbDepartment.print(JTable.PrintMode.FIT_WIDTH,headerFormat, footerFormat);
        } catch (PrinterException ex) {
            Logger.getLogger(Bank.class.getName()).log(Level.SEVERE, null, ex);
        }
      
    }//GEN-LAST:event_btnExportDepartmentActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddDept;
    private javax.swing.JButton btnDeleteDept;
    private javax.swing.JButton btnExitDept;
    private javax.swing.JButton btnExportDepartment;
    private javax.swing.JButton btnSaveDept;
    private javax.swing.JButton btnUpdateDept;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbDepartment;
    private javax.swing.JTextArea txtDeptDescription;
    private javax.swing.JTextField txtDeptName;
    // End of variables declaration//GEN-END:variables
    
    DefaultTableModel modelDept;
    
    private void initControl() {
        modelDept = (DefaultTableModel)tbDepartment.getModel();
        DepartmentList  dl = new DepartmentList();
        for (DepartmentEntity item : dl.getAllDepartment()) {
            modelDept.addRow(item.toVector());
        }
        
    }

   

   }
